/* eslint-disable @typescript-eslint/ban-ts-comment */
import { request } from '../request'

/** get role list */
export function fetchGetRoleList(params?: Api.SystemManage.RoleSearchParams) {
  return request<Api.SystemManage.RoleList>({
    url: '/role',
    method: 'get',
    params
  })
}

/**
 * get all roles
 *
 * these roles are all enabled
 */
export function fetchGetAllRoles() {
  return request<Api.SystemManage.AllRole[]>({
    url: '/systemManage/getAllRoles',
    method: 'get'
  })
}

/** get user list */
export function fetchGetUserList(params?: Api.SystemManage.UserSearchParams) {
  return request<Api.SystemManage.UserList>({
    url: '/user',
    method: 'get',
    params
  })
}

/** get menu list */
export const fetchGetMenuList = () =>
  request<Api.SystemManage.Menu[]>({
    url: '/route',
    method: 'get'
  })
    .then(response => {
      const menus = response.data || []
      return {
        data: {
          records: menus,
          total: menus.length,
          current: 1,
          size: menus.length
        },
        error: null
      }
    })
    .catch(error => {
      return {
        data: null,
        error: error.message
      }
    })

/** get all pages */
export function fetchGetAllPages() {
  return request<string[]>({
    url: '/route/getAllPages',
    method: 'get'
  })
}

/** get menu tree */
export function fetchGetMenuTree() {
  return request<Api.SystemManage.MenuTree[]>({
    url: '/route/getMenuTree',
    method: 'get'
  })
}

// 获取角色的路由权限
export function fetchRoleRoutes(roleId: string) {
  return request<number[]>({
    url: `/route/auth-route/${roleId}`,
    method: 'get'
  })
}

// 角色分配路由权限
export function authRouteWithRole(roleId: string, routeIds: number[]) {
  return request({
    url: `/authorization/assign-routes`,
    method: 'post',
    data: {
      roleId,
      routeIds: routeIds.join(',')
    }
  })
}

export type RoleModel = Pick<Api.SystemManage.Role, 'name' | 'code' | 'description' | 'status'>

function roleModelFormate(role: RoleModel) {
  const result = {
    pid: role.pid || '0',
    name: role.name,
    code: role.code,
    description: role.description,
    status: role.status
  }

  // @ts-ignore
  if (role.id) {
    // @ts-ignore
    result.id = role.id
  }

  return result
}

/**
 * 创建角色
 *
 * @param req 角色实体
 * @returns nothing
 */
export function createRole(req: RoleModel) {
  return request({
    url: '/role',
    method: 'post',
    data: roleModelFormate(req)
  })
}

/**
 * 更新角色
 *
 * @param req 角色实体
 * @returns nothing
 */
export function updateRole(req: RoleModel) {
  const { id, ...rest } = req
  return request({
    url: `/role/${id}`,
    method: 'put',
    data: roleModelFormate(rest)
  })
}

/**
 * 删除角色
 *
 * @param id 删除ID
 * @returns nothing
 */
export function deleteRole(id: string) {
  return request({
    url: `/role/${encodeURIComponent(id)}`,
    method: 'delete'
  })
}

export type RouteModel = Pick<
  Api.SystemManage.Menu,
  | 'menuType'
  | 'menuName'
  | 'routeName'
  | 'routePath'
  | 'component'
  | 'sequence'
  | 'i18nKey'
  | 'icon'
  | 'iconType'
  | 'status'
  | 'pid'
  | 'keepAlive'
  | 'constant'
  | 'href'
  | 'hideInMenu'
  | 'activeMenu'
  | 'multiTab'
  | 'fixedIndexInTab'
>

function routeModelFormate(route: RouteModel) {
  const result = {
    menu_type: route.menuType,
    menu_name: route.menuName,
    icon_type: route.iconType,
    icon: route.icon,
    route_name: route.routeName,
    route_path: route.routePath,
    component: route.component,
    path_param: route.pathParam,
    status: route.status,
    active_menu: route.activeMenu,
    hide_in_menu: route.hideInMenu,
    pid: route.pid,
    sequence: route.sequence,
    i18n_key: route.i18nKey,
    keep_alive: route.keepAlive,
    constant: route.constant,
    href: route.href,
    multi_tab: route.multiTab,
    fixed_index_in_tab: route.fixedIndexInTab,
    buttons: route.buttons,
    query: route.query
  }

  // @ts-ignore
  if (route.id) {
    // @ts-ignore
    result.id = route.id
  }

  return result
}

/**
 * 创建路由
 *
 * @param req 路由实体
 * @returns nothing
 */
export function createRoute(req: RouteModel) {
  return request({
    url: '/route',
    method: 'post',
    data: routeModelFormate(req)
  })
}

/**
 * 更新路由
 *
 * @param req 路由实体
 * @returns nothing
 */
export function updateRoute(req: RouteModel) {
  const { id, ...rest } = req
  return request({
    url: `/route/${id}`,
    method: 'put',
    data: routeModelFormate(rest)
  })
}

/**
 * 删除路由
 *
 * @param id 路由ID
 * @returns nothing
 */
export function deleteRoute(id: string) {
  return request({
    url: `/route/${encodeURIComponent(id)}`,
    method: 'delete'
  })
}

export type UserModel = Pick<
  Api.SystemManage.User,
  'username' | 'password' | 'domain' | 'nickName' | 'phoneNumber' | 'email' | 'status'
>

function userModelFormate(user: UserModel) {
  const result = {
    username: user.username,
    password: user.password,
    domain: user.domain,
    build_in: user.buildIn,
    avatar: user.avatar,
    nick_name: user.nickName,
    phone_number: user.phoneNumber,
    email: user.email,
    status: user.status
  }

  // @ts-ignore
  if (user.id) {
    // @ts-ignore
    result.id = user.id
  }

  return result
}

/**
 * 创建用户
 *
 * @param req 用户实体
 * @returns nothing
 */
export function createUser(req: UserModel) {
  return request({
    url: '/user',
    method: 'post',
    data: userModelFormate(req)
  })
}

/**
 * 更新用户
 *
 * @param req 用户实体
 * @returns nothing
 */
export function updateUser(req: UserModel) {
  const { id, ...rest } = req
  return request({
    url: `/user/${id}`,
    method: 'put',
    data: userModelFormate(rest)
  })
}

/**
 * 删除用户
 *
 * @param id 删除ID
 * @returns nothing
 */
export function deleteUser(id: string) {
  return request({
    url: `/user/${encodeURIComponent(id)}`,
    method: 'delete'
  })
}
